import 'package:flutter/material.dart';
import 'package:like_chat/entity/user.dart';
import 'package:like_chat/pages/home_page.dart';
import 'dart:io';
import 'package:dio/dio.dart';
import 'package:like_chat/widget/mybutton_widget.dart';

class LoginPage extends StatefulWidget {
  final VoidCallback voidCallback;

  const LoginPage(this.voidCallback);

  @override
  _LoginPageState createState() => _LoginPageState();
}

class _LoginPageState extends State<LoginPage> {
  TextEditingController _phoneController;
  TextEditingController _pwdController;
  String _phoneError = null;
  String _pwdError = null;
  bool _isLoading = false;
  bool _colorFlag = false;

  @override
  void initState() {
    super.initState();
    _phoneController = TextEditingController();
    _pwdController = TextEditingController();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: SingleChildScrollView(
          child: Center(
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.center,
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            const SizedBox(
              height: 100.0,
            ),
            FlutterLogo(
              colors: _colorFlag ? Colors.orange : Colors.grey,
              size: 120.0,
            ),
            const SizedBox(
              height: 30.0,
            ),
            _InputWidget(name: "手机号", icon: Icon(Icons.phone_android)),
            _InputWidget(name: "密码", icon: Icon(Icons.lock)),
            const SizedBox(
              height: 15.0,
            ),
            MyLoadingButton(
              child: Text("登 录"),
              color: _colorFlag ? Colors.orange : Colors.grey[300],
              width: 200.0,
              height: 40.0,
              onTap: _onLogin,
              isLoading: _isLoading,
              radius: 20.0,
            ),
            const SizedBox(
              height: 10.0,
            ),
            SizedBox(
              width: 200.0,
              height: 40.0,
              child: FlatButton(onPressed: _onRegister, child: Text("注 册")),
            )
          ],
        ),
      )),
    );
  }

  Widget _InputWidget({String name, Icon icon}) {
    bool flag = name == "密码";
    return SizedBox(
      width: 300.0,
      height: 80.0,
      child: TextField(
          controller: flag ? _pwdController : _phoneController,
          obscureText: flag,
          onChanged: flag ? _onChangedPwd : _onChangePhone,
          decoration: InputDecoration(
            icon: icon,
            labelText: name,
            errorText: flag ? _pwdError : _phoneError,
          )),
    );
  }

  void _onChangedPwd(String pwd) {
    if(pwd.isEmpty) {
      setState(() {
        _colorFlag = false;
      });
      return;
    }
    if (!_phoneController.text?.isEmpty) {
      setState(() {
        _colorFlag = true;
      });
    } else {
      setState(() {
        _colorFlag = false;
      });
    }
  }

  void _onChangePhone(String phone) {
    if(phone.isEmpty) {
      setState(() {
        _colorFlag = false;
      });
      return;
    }
    if (!_pwdController.text?.isEmpty) {
      setState(() {
        _colorFlag = true;
      });
    } else {
      setState(() {
        _colorFlag = false;
      });
    }
  }

  void _onLogin() {
    if (_phoneController.text?.isEmpty || _pwdController.text?.isEmpty) {
      return;
    }

    // 说明两个都输入了
    setState(() {
      _colorFlag = true;
    });

    if (_isLoading) {
      return;
    }

    // widget.voidCallback();
    setState(() {
      _isLoading = true;
      _colorFlag = false;
    });

    Future.delayed(Duration(seconds: 3), () {
      setState(() {
        _isLoading = false;
        _colorFlag = true;
      });
    });
  }

  void _onRegister() {}
}
